package com.youhome.business.controller;


import com.github.pagehelper.PageInfo;
import com.youhome.business.dto.DTOMapSearch;
import com.youhome.business.dto.DTOSearch;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.HJPOICategory;
import com.youhome.service.model.HotCityHouseCount;
import com.youhome.service.model.HouseMapShort;
import com.youhome.service.model.ListHubList;
import com.youhome.service.parameter.ParamHouseMapSearch;
import com.youhome.service.parameter.ParamHouseSearch;
import com.youhome.service.response.BrokerageResult;
import com.youhome.service.response.HoaFeeSchoolGradeResult;
import com.youhome.service.response.InterestPointResult;
import com.youhome.service.service.*;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/05/09.
 */
@RestController
@RequestMapping(value = "/house/")
@Api(tags = "房源")
public class HouseController {
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private ListHubPhotoService listHubPhotoService;
    @Autowired
    private SolrService solrService;
    @Autowired
    private HomeJunctionService homeJunctionService;
    @Autowired
    private HotCityService hotCityService;
    @Autowired
    private BrokerageService brokerageService;


    @ApiOperation(value = "房源邮编", notes = "房源邮编", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/zip/{houseId}")
    public Object zip(@PathVariable("houseId") String houseId) {
        return new Result(ResultCode.SUCCESS, listHubService.selectZipByPrimaryKey(houseId));
    }

    @ApiOperation(value = "房源基本信息", notes = "房源基本信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/basic/info/{houseId}")
    public Object basicInfo(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectBasicInfo(houseId));
    }

    @ApiOperation(value = "房源基本信息", notes = "房源基本信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/base/{houseId}")
    public Object base(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectBaseByPrimaryKey(houseId));
    }

    @ApiOperation(value = "房源特征信息", notes = "房源特征信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/characteristics/{houseId}")
    public Object characteristics(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectCharacteristics(houseId));
    }

    @ApiOperation(value = "房源图片", notes = "房源图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/photo/{houseId}")
    public Object photo(@PathVariable("houseId") String houseId) {
        return new Result(ResultCode.SUCCESS, listHubPhotoService.selectPhotoByHouseId(houseId));
    }

    @ApiOperation(value = "房源搜索", notes = "房源搜索", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/search")
    public Object search(@ApiParam @Validated DTOSearch dtoSearch) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        ParamHouseSearch map = modelMapper.map(dtoSearch, ParamHouseSearch.class);
        PageInfo<ListHubList> listHubListPageInfo = solrService.searchByDTOSearch(map);
        return new ResultPage(ResultCode.SUCCESS, listHubListPageInfo);
    }

    @ApiOperation(value = "房源地图搜索", notes = "房源地图搜索", response = HouseMapShort.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/search/map/short")
    public Object searchMapShort(@ApiParam @Validated DTOMapSearch dtoMapSearch) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(ResultCode.SUCCESS, solrService.searchMapShortByBean(modelMapper.map(dtoMapSearch, ParamHouseMapSearch.class)));
    }

    @ApiOperation(value = "搜索联想", notes = "搜索联想", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "action", value = "查询方法(zip,address,city)", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "keyword", value = "keyword", required = true, paramType = "path")
    })
    @GetMapping("/associate/{action}/{keyword}")
    public Object associate(@PathVariable(value = "action") String action, @PathVariable(value = "keyword") String keyword) throws CustomException {
        return new Result(ResultCode.SUCCESS, solrService.associateSearch(action, keyword));
    }

    @ApiOperation(value = "周边景点信息", notes = "周边景点信息", response = InterestPointResult.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "polygon", value = "边界", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "dist", value = "点与点间的距离", required = false, paramType = "query")
    })
    @GetMapping(value = "/poi")
    public Object poi(String polygon, Integer dist) {
        return new Result(ResultCode.SUCCESS, solrService.selectPoiByCoordinateAndDist(polygon, dist));
    }

    @ApiOperation(value = "poi类别", notes = "poi类别", response = HJPOICategory.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/poi/category")
    public Object poiCategory() {
        return new Result(ResultCode.SUCCESS, homeJunctionService.selectAllHJPOICategory());
    }

    @ApiOperation(value = "十大热门城市房源数量", notes = "十大热门城市房源数量", response = HotCityHouseCount.class, responseContainer = "List", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/hot/city/house/count")
    public Object hotCityCount() {
        return new Result<>(ResultCode.SUCCESS, hotCityService.selectHotCityHouseCount());
    }

    @ApiOperation(value = "物业费与学校评分", notes = "物业费与学校评分", response = HoaFeeSchoolGradeResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    @GetMapping(value = "/hoa_fee_school_grade/{houseId}")
    public Object hoaFeeSchoolGrade(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubService.selectHoaFeeSchoolGrade(houseId));
    }

    @ApiOperation(value = "经纪人", notes = "经纪人", response = BrokerageResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    @GetMapping(value = "/brokerage/{houseId}")
    public Object brokerage(@PathVariable("houseId") String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, brokerageService.selectByHouseId(houseId));
    }

}
